package com.loveyukari.dao.impl;

import com.loveyukari.dao.ShieldAuthDao;
import com.loveyukari.model.ShieldAuth;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class JdbcShieldAuthDao extends BaseDao implements ShieldAuthDao {
    private RowMapper<ShieldAuth> shieldAuthRowMapper = new RowMapper<ShieldAuth>() {
        @Override
        public ShieldAuth mapRow(ResultSet resultSet, int i) throws SQLException {
            ShieldAuth shieldAuth = new ShieldAuth();
            shieldAuth.setId(resultSet.getInt("id"));
            shieldAuth.setAuthId(resultSet.getInt("auth_id"));
            shieldAuth.setEndDate(resultSet.getTimestamp("end_time"));
            return shieldAuth;
        }
    };
    @Override
    public List<ShieldAuth> selectAll() {
        String sql = "select * from shield_auth";
        List<ShieldAuth> list = this.getJdbcTemplate().query(sql, shieldAuthRowMapper);
        return list;
    }

    @Override
    public ShieldAuth select(Integer authId) {
        String sql = "select * from shield_auth where auth_id = ?";
        List<ShieldAuth> list = this.getJdbcTemplate().query(sql, shieldAuthRowMapper, authId);
        if (list.isEmpty()){
            return null;
        }
        return list.get(0);
    }

    @Override
    public void insert(ShieldAuth shieldAuth) {
        String sql = "insert shield_auth(auth_id,end_time) values(?,?)";
        this.getJdbcTemplate().update(sql,shieldAuth.getAuthId(),shieldAuth.getEndDate());
    }

    @Override
    public void delete(int id) {
        String sql = "delete from shield_auth where id = ?";
        this.getJdbcTemplate().update(sql,id);
    }
}
